1535C - Unstable String - CodeForces Solution


binary search dp greedy implementation strings two pointers *1400

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    s = input()
    n = len(s)
    ans = 0
    i = 0
    count = 0
    while i < n and s[i] == '?':
        i += 1
        count += 1
    while i < n:
        if i+1 < n and s[i+1] == '?':
            j = i+1
            qs = 0
            while j < n and s[j] == '?':
                qs += 1
                j += 1
            if j < n:
                if (qs & 1 and s[i] == s[j]) or (qs & 1 == 0 and s[i] != s[j]):
                    count += qs + 1
                else:
                    count += 1
                    ans += (count*(count + 1))//2 + qs*count
                    count = qs
            else: count += qs + 1
            i = j
        elif i+1 < n and s[i+1] != s[i]:
            count += 1
            i += 1
        else:
            count += 1
            ans += (count*(count + 1))//2 
            count = 0
            i += 1
    ans += (count*(count + 1))//2 
    print(ans)

C++ Code:

#include <bits/stdc++.h>
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2")

using namespace std;

typedef pair<int, int> PII;
typedef long long ll;
using vi = vector<int>;
using vl = vector<long long>;
const ll MOD = 998244353 ;
const int dx[4]{1, 0, -1, 0}, dy[4]{0, 1, 0, -1}; // for every grid problem!!

#define MP make_pair
#define vt vector
#define pb push_back
#define rp(i, e) for (int (i) = 0; (i) < (int) (e); (i)+=1)
#define rp2(i, b, e) for (int (i) = b; (i) < (int) (e); (i)+=1)
#define in(x, e) (x.find(e) != x.end())
#define inpr(a, n) for (int ar = 0; ar < n; ar++) {cin >> a[ar];}
#define pa(a, pref, n) pref[0] = 0; rp(i, n) pref[i+1] = pref[i] + a[i]
#define car(a) cout << "[ "; for(auto i: a) {cout << i << ' ';} cout << "] ";
#define all(a) a.begin(), a.end()
#define fs first
#define sc second

void solve(){ 
	string s; cin >> s;
	int n = s.size();
	
	int last = 2;
	int l = 0;
	ll re = 0;
	int lq = 0;
	rp(i, n){
		if(s[i] == '?'){
			if(last != 2) last ^= 1;
			lq++;
		} 
		else{
			int cu = s[i] - '0';
			if(last == cu) l = i-lq;
			last = cu;
			lq = 0;
		}
		re += i - l + 1;
	}
	cout << re << endl;

}

//COUT << SETPRECISION(30) PLEASE 
//USE DP[2] PLEASE 
//USE DP[2] PLEASE 

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout << setprecision(30);
	int t; cin >> t; while(t--) 
	solve();

	return 0;
}


Comments

Submit
0 Comments
More Questions

1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK
1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm
732A - Buy a Shovel
1220C - Substring Game in the Lesson
452A - Eevee
1647B - Madoka and the Elegant Gift
1408A - Circle Coloring
766B - Mahmoud and a Triangle
1618C - Paint the Array
469A - I Wanna Be the Guy
1294A - Collecting Coins
1227A - Math Problem
349A - Cinema Line
47A - Triangular numbers
1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username
49A - Sleuth
1541A - Pretty Permutations